package com.genius.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.genius.annotation.Log;
import com.genius.annotation.PreFuncAuth;
import com.genius.core.JSONResult;
import com.genius.enums.BusinessType;
import com.genius.service.DictService;
import com.genius.system.bo.*;
import com.genius.system.pojo.Dict;
import com.genius.utils.PageUtils;
import com.genius.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "字典管理接口", tags = {"用于字典管理的接口"})
@RequestMapping("dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @PostMapping("/selectDictById")
    @ApiOperation(value = "查询字典（根据ID查询）",notes = "根据ID查询字典", httpMethod = "POST")
    @PreFuncAuth("SYS:DICT:SELECT")
    public JSONResult selectDictById(@Valid @RequestBody SelectDictByIdBO selectDictByIdBO) {
        Dict dict = dictService.selectDictById(selectDictByIdBO.getDictId());
        Map<String, Object> result = new HashMap<>();
        result.put("dict",dict);
        return JSONResult.ok(result);
    }

    @PostMapping("/selectDictByPage")
    @ApiOperation(value = "查询字典（分页查询）",notes = "查询字典分页数据", httpMethod = "POST")
    @PreFuncAuth("SYS:DICT:SELECT")
    public JSONResult selectDictByPage(@Valid @RequestBody SelectDictByPageBO selectDictByPageBO) {
        int pageNum = selectDictByPageBO.getPageNum();
        int pageSize = selectDictByPageBO.getPageSize();
        Dict dict = JSONUtil.parse(selectDictByPageBO).toBean(Dict.class);
        dict.setCreateBy(SecurityUtils.getUsername());
        PageUtils.startPage(pageNum,pageSize);
        List<Dict> dictList= dictService.selectDictByPage(dict);
        Map<String,Object> result = PageUtils.getResultTableData(dictList);
        return JSONResult.ok(result);
    }

    @PostMapping("/insertDict")
    @ApiOperation(value = "添加字典",notes = "添加字典", httpMethod = "POST")
    @PreFuncAuth("SYS:DICT:INSERT")
    public JSONResult insertDict(@Valid @RequestBody InsertDictBO insertDictBO) {
        Dict dict = JSONUtil.parse(insertDictBO).toBean(Dict.class);
        Long dictId = IdUtil.getSnowflake().nextId();
        dict.setDictId(dictId);
        dict.setCreateBy(SecurityUtils.getUsername());
        int rows = dictService.insertDict(dict);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    @PostMapping("/updateDict")
    @ApiOperation(value = "修改字典",notes = "修改字典", httpMethod = "POST")
    @PreFuncAuth("SYS:DICT:UPDATE")
    public JSONResult updateDict(@Valid @RequestBody UpdateDictBO updateDictBO) {
        Dict dict = JSONUtil.parse(updateDictBO).toBean(Dict.class);
        dict.setUpdateBy(SecurityUtils.getUsername());
        int rows = dictService.updateDict(dict);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    @PostMapping("/deleteDictByIds")
    @ApiOperation(value = "删除字典",notes = "删除字典", httpMethod = "POST")
    @PreFuncAuth("SYS:DICT:DELETE")
    @Log(title = "删除字典", businessType = BusinessType.DELETE)
    public JSONResult deleteDictByIds(@Valid @RequestBody DeleteDictByIdsBO deleteDictByIdsBO) {
        int rows = dictService.deleteDictByIds(deleteDictByIdsBO.getDictIds());
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

}
